package com.loan.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loan.common.core.domain.PageQuery;
import com.loan.common.core.page.TableDataInfo;
import com.loan.system.domain.BankEntity;
import com.loan.system.domain.SmsConfigEntity;
import com.loan.system.mapper.SmsConfigMapper;
import com.loan.system.service.SmsConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author shiguang on 2022/9/15 11:50
 */
@Service
@RequiredArgsConstructor
public class SmsConfigServiceImpl implements SmsConfigService {

    private final SmsConfigMapper smsConfigMapper;

    @Override
    public TableDataInfo<SmsConfigEntity> selectPageList(SmsConfigEntity entity, PageQuery pageQuery) {
        Page<SmsConfigEntity> page = smsConfigMapper.selectPageList(pageQuery.build(), this.buildQueryWrapper(entity));
        return TableDataInfo.build(page);
    }

    /**
     * 新增配置
     * @param entity
     * @return
     */
    @Override
    public int insertSmsConfig(SmsConfigEntity entity) {
        //查询该类型是否有开启中的
        SmsConfigEntity smsConfigEntity = smsConfigMapper.selectOne(new QueryWrapper<SmsConfigEntity>().eq("type", entity.getType()).eq("status", "0").eq("del_flag", "0"));
        if (smsConfigEntity!= null){
            entity.setStatus("1");
        }
        return smsConfigMapper.insert(entity);
    }

    @Override
    public int updateSmsConfig(SmsConfigEntity entity) {
        return smsConfigMapper.updateById(entity);
    }

    @Override
    public int deleteSmsConfigById(Long id) {
        return smsConfigMapper.deleteById(id);
    }

    @Override
    public int updateSmsConfigStatus(SmsConfigEntity entity) {
        if (entity.getStatus().equals("0")){
            SmsConfigEntity smsConfigEntity = smsConfigMapper.selectOne(new QueryWrapper<SmsConfigEntity>()
                    .ne("id",entity.getId())
                    .eq("type", entity.getType())
                    .eq("status", "0")
                    .eq("del_flag", "0"));
            if (smsConfigEntity !=null){
                smsConfigEntity.setStatus("1");
                smsConfigMapper.updateById(smsConfigEntity);
            }
        }
        return smsConfigMapper.updateById(entity);
    }

    @Override
    public SmsConfigEntity selectSmsConfigById(Long id) {
        return smsConfigMapper.selectById(id);
    }

    private Wrapper<SmsConfigEntity> buildQueryWrapper(SmsConfigEntity entity) {
        Map<String, Object> params = entity.getParams();
        QueryWrapper<SmsConfigEntity> wrapper = Wrappers.query();
        wrapper.eq("del_flag",0).eq(StringUtils.isNotBlank(entity.getType()),"type",entity.getType()).orderByDesc("create_time");
        return wrapper;
    }
}
